<template>
  <div class="app-container">
    <el-form ref="form" :model="form" label-width="120px">
      ID：{{ this.$route.params.id }}
      <el-form-item label="姓名" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio-group v-model="form.sex">
          <el-radio :label="0">男</el-radio>
          <el-radio :label="1">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input-number v-model.number="form.age" />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="form.phone" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">编辑</el-button>
        <el-button @click="onCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { get, update } from '@/api/fundUser'

export default {
  data() {
    return {
      form: {
        id: null,
        name: null,
        sex: 0,
        age: 0,
        phone: null,
        email: null
      }
    }
  },
  created() {
    this.fetchDataById()
  },
  methods: {
    fetchDataById() {
      var vm = this
      vm.form.id = this.$route.params.id
      get(vm.form.id).then(response => {
        vm.form.name = response.data.name
        vm.form.sex = response.data.sex
        vm.form.age = response.data.age
        vm.form.phone = response.data.phone
        vm.form.email = response.data.email
      })
    },
    onSubmit() {
      var vm = this
      var id = vm.$route.params.id
      var user = vm.form
      update(id, user).then(response => {
        vm.$message.success(response.message)
        this.$router.go(-1)
      })
    },
    onCancel() {
      // this.$store.dispatch('delVisitedViews', this.$route)
      this.$router.go(-1)
    }
  }
}
</script>

<style scoped>
.line{
  text-align: center;
}
</style>

